<?php

/**
 * 文件注释
 *
 * Author: Liam
 * Datetime: 11/26/17 00:00 PM
 */

namespace ApiBundle\Controller;

use BaseBundle\Controller\BaseController;
use Symfony\Component\HttpFoundation\Request;

class ApiController extends BaseController
{
    /**
     * 注册
     *
     * @param Request $request
     * @return \Symfony\Component\HttpFoundation\JsonResponse
     */
    public function registerAction(Request $request)
    {
        $phone = $request->get('phone');
        $password = $request->get('password');
        $code = $request->get('code');

        $new_user = array(
            'phone' => $phone,
            'password' => sha1(md5($password)),
            'code' => $code,
            'created_at' => date('y-m-d h:i:s',time())
        );

        $conn = $this->get('database_connection');

        $user_id = $conn->fetchColumn("SELECT id FROM user WHERE mobile = ?", array($phone));
        if ($user_id) {
            return $this->jsonResponse('该手机号已存在', 1);
        }

        $result = $conn->insert("user",$new_user);
        if($result){
            return $this->jsonResponse('注册成功', 0);
        }
    }

    /**
     * 登录
     *
     * @param Request $request
     * @return \Symfony\Component\HttpFoundation\JsonResponse
     */
    public function loginAction(Request $request)
    {
        $mobile = $request->get('mobile');
        $password = $request->get('password');

        $conn = $this->get('database_connection');
        $data = $conn->fetchAssoc("SELECT * FROM user WHERE mobile = '". $mobile ."'");

        if (!$data) {
            return $this->jsonResponse('该手机号未注册', 1);
        }

        if($data['status'] === 0){
            return $this->jsonResponse(1,'帐号已被禁用，请联系管理员');
        }

        if ($data['mobile'] !== $mobile || $data['password'] !== sha1(md5($password))) {
            return $this->jsonResponse('手机号与密码不匹配', 1);
        }

        $token = sha1(md5(rand(1, 999999)));
        $this->get('session')->set('token', $token);

        $conn->update("user",array("token" => $token),array("id" => $data['id']));

        return $this->jsonResponse('登录成功', 0, array('token'=> $token));
    }

    /**
     * 获取Token
     *
     * @param Request $request
     * @return \Symfony\Component\HttpFoundation\JsonResponse
     */
    public function getTokenAction(Request $request)
    {
        $token = $request->get('token');

        $conn = $this->get('database_connection');
        $data = $conn->fetchAssoc("SELECT * FROM user WHERE token = '". $token ."'");

        if ($token == $data['token']) {
            return $this->jsonResponse('操作成功', 0, $data);
        } else {
            return $this->jsonResponse('登录失效，请重新登录', 5);
        }
    }

    /**
     * 退出登录
     *
     * @param Request $request
     * @return \Symfony\Component\HttpFoundation\JsonResponse
     */
    public function exitLoginAction(Request $request)
    {
        //  获取token
        $token = $request->get('token');

        $conn = $this->get('database_connection');
        $data = $conn->fetchAssoc("SELECT * FROM user WHERE token = '". $token ."'");

        if ($token == $data['token']) {
            $conn->update("user", array("token" => NULL), array("token" => $token));
            return $this->jsonResponse('退出登录成功', 0);
        } else {
            return $this->jsonResponse('登录失效，请重新登录', 10);
        }
    }
}